﻿#ifndef __SS_CSV_DATA_H__
#define __SS_CSV_DATA_H__
#include "../ssCfgData.h"
#include "ssReadCsv.h"


namespace StarSeeker
{
//表基类
/*template<typename T>
class ssICsvTable : public ssICfgTable<T>
{
public:
	typedef ssICfgTable<T> BaseClass;
	typedef typename ssICfgTable<T>::iterator iterator;

	ssICsvTable()
	{
	}
	virtual~ssICsvTable()
	{
	}
	//读文件
	virtual bool Read()
	{
		ssReadCsv csv;
		if (!csv.LoadCsvFromFile(BaseClass::m_path.c_str()))
		{
			printf("%s read error!\n", BaseClass::m_path.c_str());
			return false;
		}

		for (u32 i = 0; i < csv.LineCount(); ++i)
		{
			ssString::StrElements_t* vec= csv.GetLine(i);
			if (vec)
			{
				T row;
				for (u32 j = 0; j < vec->Count(); ++j)
				{
					ssICfgValue* val = row.GetValue();
					if (!val) break;
					switch (val->type)
					{
					case SS_CFG_DATA_INT8:
						((ssCfgInt8*)val)->val = (*vec)[j].ToInt32();
						break;
					case SS_CFG_DATA_INT32:
						((ssCfgInt32*)val)->val = (*vec)[j].ToInt32();
						break;
					case SS_CFG_DATA_STRING:
						((ssCfgStr*)val)->val = (*vec)[j];
						break;
					case SS_CFG_DATA_FLOAT:
						((ssCfgFloat*)val)->val = (float)(*vec)[j].ToFloat();
						break;
					case SS_CFG_DATA_INT32_V:
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.Count(); ++ii)
						{
							((ssCfgVInt32*)val)->val.push_back(v[ii].ToInt32());
						}
					}
						break;
					case SS_CFG_DATA_STRING_V:
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.Count(); ++ii)
						{
							((ssCfgVStr*)val)->val.push_back(v[ii]);
						}
					}
						break;
					case SS_CFG_DATA_FLOAT_V:
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.Count(); ++ii)
						{
							((ssCfgFloat*)val)->val.push_back((float)v[ii].ToFloat());
						}
					}
						break;
					case SS_CFG_DATA_INT32_VV:
					{
						ssString::StrElementList_t vv;
						(*vec)[j].StringSplit('^', '|', vv);
						for (u32 ii = 0; ii < vv.Count(); ++ii)
						{
							std::vector<i32> v;
							for (u32 iii = 0; iii < vv[ii].Count(); ++iii)
							{
								v.push_back(vv[ii].ToInt32());
							}
							((ssCfgVVInt32*)val)->val.push_back(v);
						}
					}
						break;
					case SS_CFG_DATA_STRING_VV:
					{
						ssString::StrElementList_t vv;
						(*vec)[j].StringSplit('^', '|', vv);
						for (u32 ii = 0; ii < vv.Count(); ++ii)
						{
							std::vector<std::string> v;
							for (u32 iii = 0; iii < vv[ii].Count(); ++iii)
							{
								v.push_back(vv[ii]);
							}
							((ssCfgVVStr*)val)->val.push_back(v);
						}
					}
						break;
					case SS_CFG_DATA_FLOAT_VV:
					{
						ssString::StrElementList_t vv;
						(*vec)[j].StringSplit('^', '|', vv);
						for (u32 ii = 0; ii < vv.Count(); ++ii)
						{
							std::vector<f32> v;
							for (u32 iii = 0; iii < vv[ii].Count(); ++iii)
							{
								v.push_back((float)vv[ii].ToFloat());
							}
							((ssCfgVVFloat*)val)->val.push_back(v);
						}
					}
						break;
					default:
						break;
					}
					if (val->type == SS_CFG_DATA_INT8)
					{
						((ssCfgInt8*)val)->val = (*vec)[j].ToInt32();
					}
					else if (val->type == SS_CFG_DATA_INT32)
					{
						((ssCfgInt32*)val)->val = (*vec)[j].ToInt32();
					}
					else if (val->type == SS_CFG_DATA_STRING)
					{
						((ssCfgStr*)val)->val = (*vec)[j];
					}
					else if (val->type == SS_CFG_DATA_FLOAT)
					{
						((ssCfgFloat*)val)->val = (float)(*vec)[j].ToFloat();
					}
					else if (val->type == SS_CFG_DATA_INT32_V)
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.size(); ++ii)
						{
							((ssCfgVInt32*)val)->val.push_back(v[ii].ToInt32());
						}
					}
					else if (val->type == SS_CFG_DATA_STRING_V)
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.size(); ++ii)
						{
							((ssCfgVStr*)val)->val.push_back(v[ii]);
						}
					}
				}
				BaseClass::m_map[row.GetKey()] = row;
			}
		}
		AfterRead();
		return true;
	}
	virtual void AfterRead()
	{
	}
};*/

//双键表基类
template<typename T>
class ssICsvTable : public ssICfgTable<T>
{
public:
	typedef ssICfgTable<T> BaseClass;
	typedef typename ssICfgTable<T>::iterator iterator;

	ssICsvTable()
	{
	}
	virtual~ssICsvTable()
	{
	}
	//读文件
	virtual bool Read()
	{
		ssReadCsv csv;
		if (!csv.LoadCsvFromFile(BaseClass::m_path.c_str()))
		{
			printf("%s read error!\n", BaseClass::m_path.c_str());
			return false;
		}

		for (u32 i = 0; i < csv.LineCount(); ++i)
		{
			ssString::StrElements_t* vec = csv.GetLine(i);
			if (vec)
			{
				T row;
				for (u32 j = 0; j < vec->Count(); ++j)
				{
					ssICfgValue* val = row.GetValue();
					if (!val) break;
					switch (val->type)
					{
					case SS_CFG_DATA_INT8:
						((ssCfgInt8*)val)->val = (*vec)[j].ToInt32();
						break;
					case SS_CFG_DATA_INT32:
						((ssCfgInt32*)val)->val = (*vec)[j].ToInt32();
						break;
					case SS_CFG_DATA_STRING:
						((ssCfgStr*)val)->val = (*vec)[j].GetString();
						break;
					case SS_CFG_DATA_FLOAT:
						((ssCfgFloat*)val)->val = (float)(*vec)[j].ToFloat();
						break;
					case SS_CFG_DATA_INT32_V:
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.Count(); ++ii)
						{
							((ssCfgVInt32*)val)->val.push_back(v[ii].ToInt32());
						}
					}
					break;
					case SS_CFG_DATA_STRING_V:
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.Count(); ++ii)
						{
							((ssCfgVStr*)val)->val.push_back(v[ii].GetString());
						}
					}
					break;
					case SS_CFG_DATA_FLOAT_V:
					{
						ssString::StrElements_t v;
						(*vec)[j].StringSplit('|', v);
						for (u32 ii = 0; ii < v.Count(); ++ii)
						{
							((ssCfgVFloat*)val)->val.push_back((float)v[ii].ToFloat());
						}
					}
					break;
					case SS_CFG_DATA_INT32_VV:
					{
						ssString::StrElementList_t vv;
						(*vec)[j].StringSplit('^', '|', vv);
						for (u32 ii = 0; ii < vv.Count(); ++ii)
						{
							std::vector<i32> v;
							for (u32 iii = 0; iii < vv[ii].Count(); ++iii)
							{
								v.push_back(vv[ii][iii].ToInt32());
							}
							((ssCfgVVInt32*)val)->val.push_back(v);
						}
					}
					break;
					case SS_CFG_DATA_STRING_VV:
					{
						ssString::StrElementList_t vv;
						(*vec)[j].StringSplit('^', '|', vv);
						for (u32 ii = 0; ii < vv.Count(); ++ii)
						{
							std::vector<std::string> v;
							for (u32 iii = 0; iii < vv[ii].Count(); ++iii)
							{
								v.push_back(vv[ii][iii].GetString());
							}
							((ssCfgVVStr*)val)->val.push_back(v);
						}
					}
					break;
					case SS_CFG_DATA_FLOAT_VV:
					{
						ssString::StrElementList_t vv;
						(*vec)[j].StringSplit('^', '|', vv);
						for (u32 ii = 0; ii < vv.Count(); ++ii)
						{
							std::vector<f32> v;
							for (u32 iii = 0; iii < vv[ii].Count(); ++iii)
							{
								v.push_back((float)vv[ii][iii].ToFloat());
							}
							((ssCfgVVFloat*)val)->val.push_back(v);
						}
					}
					break;
					default:
						break;
					}
				}
				i64 key = row.GetKey();
				row.ClearTempData();
				BaseClass::m_map.insert(std::pair<i64, T>(key, row));
			}
		}
		AfterRead();
		return true;
	}
	virtual void AfterRead()
	{
	}
};

}
#endif

//使用例子
/*class TestCfgRow : public ssICfgRow
{
public:
	SS_CSV_DATA_BEGIN
	SS_CSV_KEY(m_id)
	SS_CSV_DATA(m_icon)
	SS_CSV_DATA(m_open)
	SS_CSV_DATA_END(TestCfgRow)

	ssCfgInt	m_id;
	ssCfgStr	m_icon;
	ssCfgStr	m_open;
};
class TestCfg : public ssICsvTable<TestCfgRow>
{
public:
	TestCfg()
	{
		m_path = "cfg/FootIcon.xml";
	}
};*/
